({
    data: {
        resultdata: {},
        dbConn: Program.data.dbConn,
        WorkId: Program.data.WorkId,
        requestBody: Program.data.requestBody,
        TaskModel: {
            model: {
                TaskId: 0,
                TaskCode: "",
                TaskName: "",
                TaskType: 0,
                BonusPoints: 0,
                BonusPointsLimit: 0,
                BonusGrowth: 0,
                BonusGrowthLimit: 0,
                Valid: 0,
                WorkId: 0,
                IsBonusPoints: 0,
                IsBonusGrowth: 0,
            },
            tablename: "Me_Task",
            key: "TaskId",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        Me_BonusRuleModel: {
            model: {
                RuleId: 0,
                TaskId: 0,
                RuleType: 0,
                PointsPercent: 0,
                GrowthPercent: 0,
                EnableStatus: 0,
                WorkId: 0
            },
            tablename: "Me_BonusRule",
            key: "RuleId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        Me_AmountRangeModel: {
            model: {
                RangeId: 0,
                RuleId: 0,
                StartAmount: 0,
                EndAmount: 0,
                BonusPoints: 0,
                BonusGrowth: 0,
                WorkId: 0
            },
            tablename: "Me_AmountRange",
            key: "RangeId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        LevelModel: {
            model: {
                "MemberLevelId": 0,
                "DelFlag": 0,
            },
            tablename: "Me_MemberLevel",
            key: "MemberLevelId",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: 0//机构ID
        },
        LevelModel2: {
            model: {
                MemberLevelId: 0,
                StartGrowth: 0,
                EndGrowth: 0,
                LevelName: '',
                LevelViceName: '',
                Logo: '',
                Memo: '',
                Sort: 0,
                Introduce: '',
                WorkId: 0,
                DelFlag: 0
            },
            tablename: "Me_MemberLevel",
            key: "MemberLevelId",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: 0//机构ID
        },
        RightModel: {
            model: {
                "RightId": 0,
                "DelFlag": 0,
            },
            tablename: "Me_LevelRight",
            key: "RightId",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: 0//机构ID
        },
        RightModel2: {
            model: {
                RightId: 0,
                RightName: '',
                RightViceName: '',
                ImgUrl: '',
                Introduce: '',
                WorkId: 0,
                DelFlag: 0
            },
            tablename: "Me_LevelRight",
            key: "RightId",
            val: "0",
            objval: {},
            where: "",
            pageinfo: {},
            WorkId: 0//机构ID
        },
        Me_LevelRightRelationModel: {
            model: {
                RelationId: 0,
                LevelId: 0,
                RightId: 0,
                WorkId: 0

            },
            tablename: "Me_LevelRightRelation",
            key: "RelationId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        Me_RightCouponRelationModel: {
            model: {
                RelationId: 0,
                CouponId: 0,
                RightId: 0,
                LevelId: 0,
                WorkId: 0

            },
            tablename: "Me_RightCouponRelation",
            key: "RelationId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        },
        Me_CouponModel: {
            model: {
                CouponId: 0,
                CouponName: "",
                CouponClass: 0,
                CouponType: 0,
                ValidType: 0,
                ValidDays: 0,
                StartTime: "yyyy-MM-dd",
                EndTime: "yyyy-MM-dd",
                CouponAmount: 0,
                CouponDiscount: 0,
                FullImpairment: 0,
                CouponCount: 0,
                CouponStatus: 0,
                PerLimit: 0,
                AdaptiveRange: 0,
                AdaptivePeople: 0,
                WorkId: 0
            },
            tablename: "Me_Coupon",
            key: "CouponId",
            val: "0",
            objval: {},
            WorkId: LobsterAPI.ProgramData('workid')
        }
    },
    main: function (funname) {
        var self = this;
        self.data.dbConn = PublicFunction.setdbconn('10.0.4.5:1433', 'CloudMember', 'sa', 'Lobster@123');
        if (funname == "coupon.getcouponlist") {
            var page = LobsterAPI.RequestQuery("page", 1);
            var limit = LobsterAPI.RequestQuery("limit", 10);
            var couponName = LobsterAPI.RequestQuery("couponName", "");
            var couponType = LobsterAPI.RequestQuery("couponType", -1);
            var couponStatus = LobsterAPI.RequestQuery("couponStatus", -1);
            var res = self.getcouponlist(page, limit, couponName, couponType, couponStatus);
            self.data.resultdata.data = res.data;
            self.data.resultdata.count = res.count;
        }
        else if (funname == "coupon.savecoupon") {
            var model = JSON.parse(self.data.requestBody);
            self.data.resultdata = self.savecoupon(model);
        }
        else if (funname == "coupon.publishcoupon") {
            var model = JSON.parse(self.data.requestBody);
            self.data.resultdata = self.publishcoupon(model);
        }
        //查询  
        else if (funname == "task.gettasklist") {
            var page = LobsterAPI.RequestQuery("page", 1);
            var limit = LobsterAPI.RequestQuery("limit", 10);
            var taskName = LobsterAPI.RequestQuery("taskName", "");
            var taskType = LobsterAPI.RequestQuery("taskType", -1);
            var res = self.gettasklist(page, limit, taskName, taskType);
            self.data.resultdata.data = res.data;
            self.data.resultdata.count = res.count;
        }
        //新增、修改
        else if (funname == "task.savetask") {
            var model = JSON.parse(self.data.requestBody);
            self.data.resultdata = self.savetask(model);
        }
        //停启用
        else if (funname == "task.validtask") {
            var model = JSON.parse(self.data.requestBody);
            self.validtask(model);
        } else if (funname == "task.gettaskcode") {
            self.data.resultdata.data = self.gettaskcode();
        }
        else if (funname == "task.getAmountRange") {
            var ruleId = LobsterAPI.RequestQuery("ruleId", 0);
            self.data.resultdata.data = self.getAmountRange(ruleId);
        } else if (funname == "member.getmemberlevlelist") {
            var page = LobsterAPI.RequestQuery("page", 1);
            var limit = LobsterAPI.RequestQuery("limit", 10);
            var levelName = LobsterAPI.RequestQuery("levelName", "");
            var res = self.getmemberlevlelist(page, limit, levelName);
            self.data.resultdata.data = res.data;
            self.data.resultdata.count = res.count;
        } else if (funname == "member.savememberlevel") {
            var model = JSON.parse(self.data.requestBody);
            self.savememberlevel(model);
        } else if (funname == "member.enable") {
            var model = JSON.parse(self.data.requestBody);
            self.enable(model);
        } else if (funname == "getUploadUrl") {
            self.data.resultdata.data = self.getUploadUrl();
        } else if (funname == "member.getlevelrightlist") {
            var page = LobsterAPI.RequestQuery("page", 1);
            var limit = LobsterAPI.RequestQuery("limit", 10);
            var rightName = LobsterAPI.RequestQuery("rightName", "");
            var res = self.getlevelrightlist(page, limit, rightName);
            self.data.resultdata.data = res.data;
            self.data.resultdata.count = res.count;
        } else if (funname == "member.getlevelrightlist2") {
            var levelId = LobsterAPI.RequestQuery("levelId", 0);
            var rightName = LobsterAPI.RequestQuery("rightName", "");
            var res = self.getlevelrightlist2(levelId, rightName);
            self.data.resultdata.data = res.data;
            self.data.resultdata.count = res.count;
        } else if (funname == "member.savelevelright") {
            var model = JSON.parse(self.data.requestBody);
            self.savelevelright(model);
        } else if (funname == "member.rightenable") {
            var model = JSON.parse(self.data.requestBody);
            self.rightenable(model);
        } else if (funname == "member.savelevelrightrelation") {
            var model = JSON.parse(self.data.requestBody);
            model.WorkId = self.data.WorkId;
            self.data.resultdata = self.savelevelrightrelation(model);
        }
        else if (funname == "member.saverightcouponrelation") {
            var model = JSON.parse(self.data.requestBody);
            model.WorkId = self.data.WorkId;
            self.data.resultdata = self.saverightcouponrelation(model);
        }
        else if (funname == "member.getrightcoupon") {
            var levelId = LobsterAPI.RequestQuery("levelId", 0);
            var rightId = LobsterAPI.RequestQuery("rightId", 0);
            var res = self.getrightcoupon(levelId, rightId);
            self.data.resultdata.data = res.data;
        } else if (funname == "member.getcoupon") {
            var res = self.getcoupon(levelId, rightId);
            self.data.resultdata.data = res.data;
        }
        return self.data.resultdata;
    },
    getcouponlist: function (page, limit, couponName, couponType, couponStatus) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "CouponId desc";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select * from Me_Coupon where WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        if ((couponName || "") != "") {
            strsql += " and CouponName like ?";
            dbparams.add("%" + couponName + "%");
        }
        if (couponType > 0) {
            strsql += " and CouponType = ?";
            dbparams.add(couponType);
        }
        if (couponStatus > 0) {
            strsql += " and CouponStatus = ?";
            dbparams.add(couponStatus);
        }
        var data = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    },
    savecoupon(model) {
        try {
            var self = this;
            LobsterAPI.TrackLog("savecoupon", model);
            if (model.FullImpairment)
                self.data.Me_CouponModel.objval.FullImpairment = model.FullImpairment;
            if (model.CouponAmount)
                self.data.Me_CouponModel.objval.FullImpairment = model.CouponAmount; 
            self.data.Me_CouponModel.objval = model;

            LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.Me_CouponModel);
        } catch (e) {
            LobsterAPI.TrackLog("savecoupon", "exception");
            return { code: 1, msg: e.message };
        }
    },
    publishcoupon(model) {
        var strsql = "update me_coupon set couponstatus=? where couponid=?"
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(model.CouponStatus);
        dbparams.add(model.CouponId);
        self.data.dbConn.executeUpdate(strsql, dbparams);
    },
    gettasklist: function (page, limit, taskName, taskType) {
        try {
            var self = this;
            var pageInfo = {};
            pageInfo.key = "TaskId";
            pageInfo.page = page;
            pageInfo.perPage = limit;
            pageInfo.totalRecord = 0;
            pageInfo.totalPage = 0;
            var strsql = "select a.*,b.RuleId,b.RuleType,b.GrowthPercent,b.PointsPercent from Me_Task a left join Me_BonusRule b on a.TaskId=b.TaskId where a.WorkId=?";
            var dbparams = Packages.java.util.ArrayList();
            dbparams.add(self.data.WorkId);
            if ((taskName || "") != "") {
                strsql += " and a.TaskName Like ? ";
                dbparams.add('%' + taskName + '%');
            }
            if (taskType > -1) {
                strsql += " and a.TaskType = ? ";
                dbparams.add(taskType);
            }
            LobsterAPI.TrackLog("gettasklist-dbConn", self.data.dbConn);
            var data = ORMFunction.GetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
            LobsterAPI.TrackLog("gettasklist", "3333");
            return { data: data, count: pageInfo.totalRecord };
        } catch (e) {
            return { code: 1, msg: e.message };
        }
    },
    savetask: function (model) {
        try {
            var self = this;
            model = JSON.parse(model.data);
            // MirthAPI.TrackLog("savetask1", model);
            // MirthAPI.TrackLog("savetask2", model.task);
            // MirthAPI.TrackLog("savetask3", model.rule);
            // MirthAPI.TrackLog("savetask4", model.rangeList);
            self.data.dbConn.setAutoCommit(false)
            self.data.TaskModel.objval = model.task;
            LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.TaskModel);
            var newtaskid = self.data.TaskModel.objval.TaskId
            MirthAPI.TrackLog("savetask5", newtaskid);
            if (model.rule != null) {
                model.rule.TaskId = newtaskid;
                self.data.Me_BonusRuleModel.objval = model.rule;
                MirthAPI.TrackLog("savetask6", model.rule);
                LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.Me_BonusRuleModel);
            }
            if (model.rangeList != null) {
                var newruleId = self.data.Me_BonusRuleModel.objval.RuleId;
                var strsql = "delete  from  Me_AmountRange where RuleId=?"
                var dbparams = Packages.java.util.ArrayList();
                MirthAPI.TrackLog("savetask7", newruleId);
                dbparams.add(newruleId);
                self.data.dbConn.executeUpdate(strsql, dbparams);
                MirthAPI.TrackLog("savetask8", "hehe");
                model.rangeList.forEach(range => {
                    range.RuleId = newruleId;
                    range.RangeId = 0;
                    self.data.Me_AmountRangeModel.objval = range;
                    MirthAPI.TrackLog("savetask9", range);
                    LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.Me_AmountRangeModel);
                });
            }
            self.data.dbConn.commit();
        } catch (e) {
            self.data.dbConn.rollback();
            return { code: 1, msg: e.message };
        }
    },
    validtask: function (model) {
        PublicFunction.TrackLog("validtask", 0, model.TaskId);
        PublicFunction.TrackLog("validtask", 0, model.Valid);
        var self = this;
        var newtask = {};
        newtask.TaskId = model.TaskId;
        newtask.Valid = model.Valid;
        self.data.TaskModel.objval = newtask;
        LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.TaskModel);
    },
    gettaskcode: function () {
        var self = this;
        var strsql = "SELECT  RIGHT('000' + CAST((ISNULL(MAX(TaskCode), 0) + 1) AS NVARCHAR(50)), 3) as TaskCode from Me_Task Where WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        return LobsterAPI.DbGetJsonObject(self.data.dbConn, strsql, dbparams);
    },
    getAmountRange: function (ruleId) {
        var self = this;
        var strsql = "select * from Me_AmountRange where RuleId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(ruleId);
        return LobsterAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams);
    },
    getmemberlevlelist: function (page, limit, levelName) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "Sort";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select * from Me_MemberLevel where WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        if ((levelName || "") != "") {
            strsql += " and (LevelName Like ? or LevelViceName Like ?)";
            dbparams.add('%' + levelName + '%');
            dbparams.add('%' + levelName + '%');
        }
        var data = LobsterAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    },
    savememberlevel(model) {
        var self = this;
        var newlevel = {};
        newlevel.MemberLevelId = model.MemberLevelId;
        newlevel.StartGrowth = model.StartGrowth;
        newlevel.EndGrowth = model.EndGrowth;
        newlevel.LevelName = model.LevelName;
        newlevel.LevelViceName = model.LevelViceName;
        newlevel.Logo = model.Logo;
        newlevel.Memo = model.Memo;
        newlevel.Sort = model.Sort;
        newlevel.Introduce = model.Introduce;
        newlevel.DelFlag = model.DelFlag;
        newlevel.WorkId = self.data.WorkId;
        self.data.LevelModel2.objval = newlevel;
        self.data.LevelModel2.WorkId = self.data.WorkId;
        LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.LevelModel2);
    },
    enable(model) {
        var self = this;
        var newlevel = {};
        newlevel.MemberLevelId = model.MemberLevelId;
        newlevel.DelFlag = model.DelFlag;
        self.data.LevelModel.objval = newlevel;
        LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.LevelModel);
    },
    getlevelrightlist: function (page, limit, rightName) {
        var self = this;
        var pageInfo = {};
        pageInfo.key = "RightId";
        pageInfo.page = page;
        pageInfo.perPage = limit;
        pageInfo.totalRecord = 0;
        pageInfo.totalPage = 0;
        var strsql = "select * from Me_LevelRight where WorkId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        if ((rightName || "") != "") {
            strsql += " and (RightName Like ? or RightViceName Like ?)";
            dbparams.add('%' + rightName + '%');
            dbparams.add('%' + rightName + '%');
        }
        var data = LobsterAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams, pageInfo);
        return { data: data, count: pageInfo.totalRecord };
    },
    getlevelrightlist2: function (levleId, rightName) {
        var self = this;
        var strsql = "select a.*,(select  top 1 b.RelationId from Me_LevelRightRelation b where b.LevelId=? and a.RightId=b.RightId) as RelationId from Me_LevelRight a \
        where a.WorkId=? and a.DelFlag=0";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(levleId);
        dbparams.add(self.data.WorkId);
        if ((rightName || "") != "") {
            strsql += " and (a.RightName Like ? or a.RightViceName Like ?)";
            dbparams.add('%' + rightName + '%');
            dbparams.add('%' + rightName + '%');
        }
        var data = LobsterAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams);
        return { data: data, count: data.length };
    },
    getrightcoupon: function (levelId, rightId) {
        var self = this;
        var strsql = "select a.* from Me_Coupon a\
        left join Me_RightCouponRelation b\
        on a.CouponId=b.CouponId\
        where a.WorkId=? and  a.CouponStatus=2\
        and ((StartTime>GETDATE() and ValidType=1) or ValidType=2)\
        and b.LevelId=? and b.RightId=?";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        dbparams.add(levelId);
        dbparams.add(rightId);
        var data = LobsterAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams);
        return { data: data };
    },
    getcoupon: function () {
        var self = this;
        var strsql = "select * from Me_Coupon where WorkId=? and CouponStatus=2\
        and ((StartTime>GETDATE() and ValidType=1) or ValidType=2)\ ";
        var dbparams = Packages.java.util.ArrayList();
        dbparams.add(self.data.WorkId);
        var data = LobsterAPI.DbGetJsonData(self.data.dbConn, strsql, dbparams);
        return { data: data };
    },
    savelevelright(model) {
        var self = this;
        var newright = {};
        newright.RightId = model.RightId;
        newright.RightName = model.RightName;
        newright.RightViceName = model.RightViceName;
        newright.ImgUrl = model.ImgUrl;
        newright.Introduce = model.Introduce;
        newright.DelFlag = model.DelFlag;
        newright.WorkId = self.data.WorkId;
        self.data.RightModel2.objval = newright;
        self.data.RightModel2.WorkId = self.data.WorkId;
        LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.RightModel2);
    },
    savelevelrightrelation(model) {
        try {
            var self = this;
            self.data.dbConn.setAutoCommit(false)
            var relationlist = JSON.parse(model.relationlist);
            var strsql = "delete  from  Me_LevelRightRelation where LevelId=? and WorkId=?"
            var dbparams = Packages.java.util.ArrayList();
            dbparams.add(model.LevelId);
            dbparams.add(self.data.WorkId);
            var ret = self.data.dbConn.executeUpdate(strsql, dbparams);
            // if (ret < 1)
            //     throw '保存失败';
            relationlist.forEach(relation => {
                relation.RelationId = 0;
                self.data.Me_LevelRightRelationModel.objval = relation;
                LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.Me_LevelRightRelationModel);
            })
            self.data.dbConn.commit();
        } catch (e) {
            self.data.dbConn.rollback();
            return { code: 1, msg: e.message };
        }

    },
    saverightcouponrelation(model) {
        try {
            var self = this;
            self.data.dbConn.setAutoCommit(false)
            var relationlist = JSON.parse(model.relationlist);
            var strsql = "delete  from  Me_RightCouponRelation where WorkId=? and LevelId=? and RightId=?"
            var dbparams = Packages.java.util.ArrayList();
            dbparams.add(self.data.WorkId);
            dbparams.add(model.LevelId);
            dbparams.add(model.RightId);
            var ret = self.data.dbConn.executeUpdate(strsql, dbparams);
            // if (ret < 1)
            //     throw '保存失败';
            relationlist.forEach(relation => {
                relation.RelationId = 0;
                self.data.Me_RightCouponRelationModel.objval = relation;
                LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.Me_RightCouponRelationModel);
            })
            self.data.dbConn.commit();
        } catch (e) {
            self.data.dbConn.rollback();
            return { code: 1, msg: e.message };
        }
    },
    rightenable(model) {
        var self = this;
        var newright = {};
        newright.RightId = model.RightId;
        newright.DelFlag = model.DelFlag;
        self.data.RightModel.objval = newright;
        LobsterAPI.OrmMySql.SaveModel(self.data.dbConn, self.data.RightModel);
    },
    getUploadUrl: function () {
        var appId = "APP20210728143245471";//LobsterAPI.ProgramData('appkey');
        var apiGateway = LobsterAPI.apiGateway;
        var url = apiGateway + "/common/v1/OSS/UploadFile" + "?appKey=" + appId;
        // PublicFunction.TrackLog("url", 0, url);
        return { url: url };
    },
})